package com.qcby.train.mybaitsplusdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.train.mybaitsplusdemo.entity.Student;
import com.qcby.train.mybaitsplusdemo.model.ResultCode;
import com.qcby.train.mybaitsplusdemo.model.ResultJson;
import com.qcby.train.mybaitsplusdemo.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: StudentController
 * @description:
 * @author: lxt
 * @create: 2021-04-26 20:38
 **/
@RestController
@RequestMapping("rest/student")
public class RestStudentController {



    @Autowired
    private IStudentService studentService;

    /**
     * @Description:
     * @param pageNo
     * @param pageSize
     * @param student
     * @return: com.qcby.train.mybaitsplusdemo.model.ResultJson 
     * @Author: lxt 
     * @Date: 2021/5/15 22:50
     */
    @RequestMapping("listPageNew")
    public ResultJson listPageNew(int pageNo,int pageSize,Student student){
        Page<Student> page = new Page<>(pageNo,pageSize);
        IPage<Student> pageInfo = studentService.listPageNew(student,page);
        return ResultJson.ok(pageInfo);
    }



    /**
     * @Description: 连表分页查询  left join
     * @param pageNo
     * @param pageSize
     * @param student
     * @return: com.qcby.train.mybaitsplusdemo.model.ResultJson 
     * @Author: lxt 
     * @Date: 2021/4/26 21:15
     */
    @RequestMapping("listPage")
    public ResultJson listPage(int pageNo,int pageSize,Student student){
        Page<Student> page = new Page<>(pageNo,pageSize);


        IPage<Student> pageInfo = studentService.listPage(page,student);
        return ResultJson.ok(pageInfo);
    }
    /**
     * @Description: 连表查询  多对一：association
     * @param pageNo
     * @param pageSize
     * @param student
     * @return: com.qcby.train.mybaitsplusdemo.model.ResultJson 
     * @Author: lxt 
     * @Date: 2021/4/26 21:35
     */
    @RequestMapping("listPage1")
    public ResultJson listPage1(int pageNo,int pageSize,Student student){
        Page<Student> page = new Page<>(pageNo,pageSize);
        IPage<Student> pageInfo = studentService.listPage1(page,student);
        return ResultJson.ok(pageInfo);
    }

    /**
     * @Description:
     * @param pageNo
     * @param pageSize
     * @param student
     * @return: com.qcby.train.mybaitsplusdemo.model.ResultJson 
     * @Author: lxt 
     * @Date: 2021/4/26 21:46
     */
    @RequestMapping("listPage2")
    public ResultJson listPage2(int pageNo,int pageSize,Student student){
        Page<Student> page = new Page<>(pageNo,pageSize);
        // lambda表达式
        LambdaQueryWrapper<Student> queryWrapper = new QueryWrapper<Student>().lambda();
        if(!StringUtils.isEmpty(student.getName())){
            queryWrapper.like(Student::getName,student.getName());
        }
        if(student.getClassInfoId() != null){
            queryWrapper.eq(Student::getClassInfo,student.getClassInfo());
//            queryWrapper.eq("class_info_id",student.getClassInfo());
        }
        IPage<Student> pageInfo = studentService.listPage2(page,queryWrapper);
        return ResultJson.ok(pageInfo);
    }
    /**
     * @Description:    不推荐
     * @param pageNo
     * @param pageSize
     * @param student
     * @return: com.qcby.train.mybaitsplusdemo.model.ResultJson 
     * @Author: lxt 
     * @Date: 2021/4/26 21:46
     */
    @RequestMapping("listPage3")
    public ResultJson listPage3(int pageNo,int pageSize,Student student){
        Page<Student> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
        if(!StringUtils.isEmpty(student.getName())){
//            queryWrapper.like(Student::getName,student.getName());
            queryWrapper.like("s.name",student.getName());
        }
        IPage<Student> pageInfo = studentService.listPage3(page,queryWrapper);
        return ResultJson.ok(pageInfo);
    }

    /**
     * @Description: 添加和更新方法
     * @param student
     * @return: com.qcby.train.mybaitsplusdemo.model.ResultJson 
     * @Author: lxt 
     * @Date: 2021/4/26 20:44
     */
    @RequestMapping("saveOrUpdate")
    public ResultJson saveOrUpdate(Student student){
        boolean r = studentService.saveOrUpdate(student);
        if (r){
            return ResultJson.ok();
        }
        return ResultJson.failure(ResultCode.SERVER_ERROR);
    }

    /**
     * @Description: 根据名字和学号更新
     * @param student
     * @return: com.qcby.train.mybaitsplusdemo.model.ResultJson 
     * @Author: lxt 
     * @Date: 2021/4/26 20:49
     */
    @RequestMapping("updateByNameAndNumber")
    public ResultJson updateByNameAndNumber(Student student){
        // 根据条件更新
        boolean r = studentService.update(student,new UpdateWrapper<Student>()
                .lambda()
                .eq(Student::getName,student.getName())
                .eq(Student::getNumber,student.getNumber())
        );
        //  直接根据id更新
        //studentService.updateById(student);
        if (r){
            return ResultJson.ok();
        }
        return ResultJson.failure(ResultCode.SERVER_ERROR);
    }

    /**
     * @Description: 根据名字和学号删除
     * @param student
     * @return: com.qcby.train.mybaitsplusdemo.model.ResultJson
     * @Author: lxt
     * @Date: 2021/4/26 20:49
     */
    @RequestMapping("deleteByNameAndNumber")
    public ResultJson deleteByNameAndNumber(Student student){
        boolean r = studentService.remove(new QueryWrapper<Student>()
                .lambda()
                .eq(Student::getName,student.getName())
                .eq(Student::getNumber,student.getNumber()));
        // 单个根据id删除
//        studentService.removeById(student.getId());
//        // 根据id批量删除
//        List<Long> idList = new ArrayList<>();
//        idList.add(1L);
//        idList.add(2L);
//        studentService.removeByIds(idList);
        if (r){
            return ResultJson.ok();
        }
        return ResultJson.failure(ResultCode.SERVER_ERROR);
    }



}
